package com.mall.web.control.app.user;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mall.biz.user.record.RecordBizImpl;
import com.mall.client.mall.MallService;
import com.mall.client.mallGroupBuy.MallGroupbBuyService;
import com.mall.client.mallGroupBuy.MallTuanCodeService;
import com.mall.client.trade.TradeService;
import com.mall.common.util.PageUtil;
import com.mall.constant.TradeTypeConstant;
import com.mall.pojo.MallGroupBuy;
import com.mall.pojo.MallTuanCode;
import com.mall.pojo.MinsuOrder;
import com.mall.pojo.Trade;
import com.mall.pojo.User;
import com.mall.query.Page;
import com.mall.query.TradeRecordQuery;
import com.mall.vo.trade.TradeVO;
import com.mall.web.annotation.UserSecurity;
import com.mall.web.util.APPCookieUtil;
import com.mall.web.util.HttpUtil;
import com.mall.web.util.WebUtil;
import com.mall.web.vo.ResponseEntity;

@Controller
@RequestMapping("/user/record/")
public class RecordMallGroupBuyController {
	
	@Autowired
	private RecordBizImpl recordBiz;
	@Autowired
	private TradeService tradeService;
	@Autowired
	private MallTuanCodeService mallTuanCodeService;
	@Autowired
	private MallGroupbBuyService mallGroupBuyService;
	@Autowired
	private MallService mallService;
	
	/**
	 * 用户团购列表页
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@UserSecurity
	@RequestMapping("mallGroupBuyTrade")
	public String mallGroupBuyTrade(HttpServletRequest request, HttpServletResponse response,ModelMap modelMap){
		tradeRecordMap(request,response,modelMap);
		return "user/record/mallGroupBuyTrade";
		
	}
	
	/**
	 * 加载更多
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@UserSecurity
	@RequestMapping("mallGroupBuyTradeList")
	public String mallGroupBuyTradeList(HttpServletRequest request, HttpServletResponse response,ModelMap modelMap){
		tradeRecordMap(request,response,modelMap);
		return "user/record/list/mallGroupBuyTradeList";
		
	}
	
	private ModelMap tradeRecordMap(HttpServletRequest request, HttpServletResponse response,ModelMap modelMap){
		Page page = new Page();
		int pageNo = HttpUtil.getIntegerValueHasDefaultFromRequest(request, "pageNo", 1);
		int pageSize = HttpUtil.getIntegerValueHasDefaultFromRequest(request, "pageSize",PageUtil.PAGE_SIZE_APP);
		page.setPageByPageNoAndPageSize(pageNo, pageSize);
		Integer onlyKey = HttpUtil.getCommkeyId(request, response);
		Integer status = HttpUtil.getIntegerValueHasDefaultFromRequest(request, "status", 0);
		User user = WebUtil.getCurrentUser(request);
		TradeRecordQuery tradeRecordQuery = new TradeRecordQuery();
		tradeRecordQuery.setOnlyKey(onlyKey);
		tradeRecordQuery.setStatus(status);
		tradeRecordQuery.setUserId(user.getId());
		tradeRecordQuery.setType(TradeTypeConstant.TYPE_TUANGOU);
		tradeRecordQuery.setPage(page);
		List<TradeVO> tradeVOList = this.recordBiz.getTradeRecordByQuery(tradeRecordQuery);
		modelMap.put("tradeVOList", tradeVOList);
		int app = APPCookieUtil.urlFromApp(request);
		modelMap.put("app", app);
		return modelMap;
	}
	/**
	 * 用户团购记录详情页
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@UserSecurity
	@RequestMapping("mallGroupBuyInfo")
	public String mallGroupBuyInfo(HttpServletRequest request, HttpServletResponse response,ModelMap modelMap){
		User user = WebUtil.getCurrentUser(request);
		Long orderId = HttpUtil.getLongValueFromRequest(request, "orderId");
		Trade trade = this.tradeService.getTradeByOrderId(orderId);
		MallGroupBuy mallGroupBuy = this.mallGroupBuyService.getMallGroupBuyById(trade.getParentId());
		List<MallTuanCode> mallTuanCodeList = this.mallTuanCodeService.getMallTuanCodeListByTradeAndUserId(user.getId(),orderId);
		int useCount = this.mallTuanCodeService.getMallTuanCodeUseCount(user.getId(),orderId);
		modelMap.put("mallGroupBuy", mallGroupBuy);
		modelMap.put("useCount", useCount);
		modelMap.put("mallTuanCodeList", mallTuanCodeList);
		modelMap.put("mall", this.mallService.getMallById(trade.getMallId()));
		modelMap.put("trade", trade);
		return "user/record/recordinfo/mallGroupBuyInfo";
	}
	
	/**
	 * 删除订单
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@UserSecurity
	@RequestMapping("deleteAction")
	@ResponseBody
	public ResponseEntity<Trade> deleteAction(HttpServletRequest request, HttpServletResponse response,ModelMap modelMap){
		Long tradeId = HttpUtil.getLongValueFromRequest(request, "tradeId");
		Trade trade = this.tradeService.getTradeByOrderId(tradeId);
		ResponseEntity<Trade> responseEntity = new ResponseEntity<>();
		if (trade == null) {
			responseEntity.initErrorResponseEntity(null, "delete error");
		}
		trade.setGmtModify(new Date());
		trade.setStatus(-8);//删除订单
		this.tradeService.updateTradeByTrade(trade);
		responseEntity.initSuccResponseEntity(trade, "delete succsee");
		return responseEntity;
	}
}
